﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YY.Admin.Core;
using static YY.Admin.Core.SysUserEvents;
using Prism.Events;
namespace YY.Admin.EventBus
{
    public class SysUserEventSubscriber : IDisposable
    {
        private readonly IEventAggregator _eventAggregator;
        private readonly ILoggerService _logger;
        private readonly List<SubscriptionToken> _subscriptions = new();
        public SysUserEventSubscriber(
            IEventAggregator eventAggregator,
            ILoggerService logger)
        {
            _eventAggregator = eventAggregator;
            _logger = logger;
            SubscribeEvents();
        }
        public void SubscribeEvents()
        {
            _eventAggregator.GetEvent<AddUserEvent>().Subscribe(OnAddUser, ThreadOption.BackgroundThread);
            _eventAggregator.GetEvent<UpdateUserEvent>().Subscribe(OnUpdateUser, ThreadOption.BackgroundThread);
            _eventAggregator.GetEvent<DeleteUserEvent>().Subscribe(OnDeleteUser, ThreadOption.BackgroundThread);
            _eventAggregator.GetEvent<SetUserStatusEvent>().Subscribe(OnSetUserStatus, ThreadOption.BackgroundThread);
            _eventAggregator.GetEvent<ChangePwdEvent>().Subscribe(OnChangePwd, ThreadOption.BackgroundThread);
            _eventAggregator.GetEvent<ResetPwdEvent>().Subscribe(OnResetPwd, ThreadOption.BackgroundThread);
            _eventAggregator.GetEvent<UnlockUserLoginEvent>().Subscribe(OnUnlockUserLogin, ThreadOption.BackgroundThread);
            _eventAggregator.GetEvent<RegisterUserEvent>().Subscribe(OnRegisterUser, ThreadOption.BackgroundThread);
            _eventAggregator.GetEvent<LoginUserEvent>().Subscribe(OnLoginUser, ThreadOption.BackgroundThread);
            _eventAggregator.GetEvent<LoginOutEvent>().Subscribe(OnLoginOut, ThreadOption.BackgroundThread);
            _eventAggregator.GetEvent<UpdateUserRoleEvent>().Subscribe(OnUpdateUserRole, ThreadOption.BackgroundThread);
        }

        public void OnAddUser(SysUser payload)
        {
            try
            {
                _logger.Information($"添加新用户: {payload.Account}");
            }
            catch (Exception ex)
            {
                _logger.Error($"添加用户事件处理失败: {ex.Message}", ex);
            }
        }

        public void OnRegisterUser(SysUser payload)
        {
            try
            {
                Task.Run(() => {
                
                
                });
                _logger.Information($"用户注册");
            }
            catch (Exception ex)
            {
                _logger.Error($"注册用户事件处理失败: {ex.Message}", ex);
            }
        }

        public void OnUpdateUser((SysUser Original, SysUser Updated) payload)
        {
            try
            {
                _logger.Information($"更新用户");
            }
            catch (Exception ex)
            {
                _logger.Error($"更新用户事件处理失败: {ex.Message}", ex);
            }
        }

        public void OnDeleteUser(SysUser payload)
        {
            try
            {
            }
            catch (Exception ex)
            {
                _logger.Error($"删除用户事件处理失败: {ex.Message}", ex);
            }
        }

        public void OnSetUserStatus((SysUser User, StatusEnum NewStatus) payload)
        {
            try
            {
            }
            catch (Exception ex)
            {
                _logger.Error($"设置用户状态事件处理失败: {ex.Message}", ex);
            }
        }
        public void OnChangePwd(SysUser payload)
        {
            try
            {
            }
            catch (Exception ex)
            {
                _logger.Error($"设置用户状态事件处理失败: {ex.Message}", ex);
            }
        }
        public void OnUpdateUserRole((SysUser User, List<long> RoleIds) payload)
        {
            try
            {
            }
            catch (Exception ex)
            {
                _logger.Error($"更新用户角色事件处理失败: {ex.Message}", ex);
            }
        }

        public void OnUnlockUserLogin(SysUser payload)
        {
            try
            {
            }
            catch (Exception ex)
            {
                _logger.Error($"解除登录锁定事件处理失败: {ex.Message}", ex);
            }
        }

        public void OnResetPwd(SysUser payload)
        {
            throw new NotImplementedException();
        }

        public void OnLoginUser(SysUser payload)
        {
            try
            {
                _logger.Information($"登录成功");
            }
            catch (Exception ex)
            {
                _logger.Error($"登录处理失败: {ex.Message}", ex);
            }
        }

        public void OnLoginOut(SysUser payload)
        {
            throw new NotImplementedException();
        }
        public void Dispose()
        {
            // 显式取消所有订阅
            foreach (var token in _subscriptions)
            {
                token.Dispose();
            }
        }
    }
}
